import Vue from "vue";
import VueRouter from "vue-router";

import Login from "@/pages/login/login";
import Layout from "@/pages/layout/layout";
import Home from "@/pages/manage/home/home";
import Content from "@/pages/manage/content/content";
import Article from "@/pages/manage/article/article";
import Fans from "@/pages/manage/fans/fans";
import Comment from "@/pages/manage/comment/comment";
import Profile from "@/pages/manage/profile/profile";
import Picture from "@/pages/manage/picture/picture";

Vue.use(VueRouter);

// 路由配置表
const routes = [
  {
    path: "",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Layout,
    children: [
      {
        path: "/home",
        component: Home,
      },
      {
        path: "/content",
        component: Content,
      },
      {
        path: "/picture",
        component: Picture,
      },
      {
        path: "/article",
        component: Article,
      },
      {
        path: "/comment",
        component: Comment,
      },
      {
        path: "/fans",
        component: Fans,
      },
      {
        path: "/profile",
        component: Profile,
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to, from, next) => {
  const user = JSON.parse(window.localStorage.getItem('user'));
  if (to.path !== "/login") {
    if (user) {
      next();
    } else {
      next('/login');
    }
  } else {
    next();
  }
});

export default router;
